# __init__.py
from flask import Flask
from flask_cors import CORS
from .api.xhs_route import xhs_bp
from .api.user import user_bp
from .services.db_operation import utils_bp
from .services.crawl_service import crawl_bp
from .api.auth import auth_bp
# from celery import Celery
from flask_jwt_extended import JWTManager
from dotenv import load_dotenv
from datetime import timedelta
import os
# def make_celery(app):
#     celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL'])
#     celery.conf.update(app.config)
#     app.celery = celery
#     return celery
load_dotenv()
def create_app():
    app = Flask(__name__)
    # app.config['CELERY_BROKER_URL'] = 'redis://localhost:6379/0'  # 配置Celery的broker URL
    #
    # # 应用Celery配置
    # app.celery = make_celery(app)
    # 设置 JWT 密钥
    # app.config['JWT_SECRET_KEY'] = 'Alvin'
    app.config['JWT_SECRET_KEY'] = os.getenv('JWT_SECRET_KEY')
    app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(minutes=15)
    app.config['JWT_REFRESH_TOKEN_EXPIRES'] = timedelta(days=30)
    app.config['JWT_TOKEN_LOCATION'] = ['headers', 'cookies']
    app.config['JWT_ACCESS_COOKIE_NAME'] = 'access_token_cookie'
    app.config['JWT_REFRESH_COOKIE_NAME'] = 'refresh_token_cookie'
    app.config['JWT_COOKIE_SECURE'] = False # 在生产环境中使用 HTTPS 时设置为 True
    app.config['JWT_COOKIE_CSRF_PROTECT'] = False

    # 初始化 JWTManager
    jwt = JWTManager(app)

    # 允许跨域请求
    CORS(app, supports_credentials=True)

    # 注册蓝图
    app.register_blueprint(xhs_bp, url_prefix='/api/xhs')
    app.register_blueprint(crawl_bp, url_prefix='/api/crawl')
    app.register_blueprint(utils_bp, url_prefix='/api/db')
    app.register_blueprint(auth_bp, url_prefix='/api/auth')
    app.register_blueprint(user_bp, url_prefix='/api/user')

    # 其他配置...
    return app